//
//  LBTwoPassTextureSamplingFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/28/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBTwoPassTextureSamplingFilter.h"
#include "LBGraphics.h"
#include "LBTexture.h"

LBTwoPassTextureSamplingFilter::LBTwoPassTextureSamplingFilter(){
    
}

void LBTwoPassTextureSamplingFilter::init(char* vShaderStr1, char* fShaderStr1, char* vShaderStr2, char* fShaderStr2){
    
    LBTwoPassFilter::init(vShaderStr1, fShaderStr1, vShaderStr2, fShaderStr2);
    LBGraphics* g = LBGraphics::getInstance();
    
    g->useProgram(program);
    verticalPassTexelWidthOffsetUniform = program->uniformIndex("texelWidthOffset");
    verticalPassTexelHeightOffsetUniform = program->uniformIndex("texelHeightOffset");
    
    g->useProgram(program2);
    horizontalPassTexelWidthOffsetUniform = program2->uniformIndex("texelWidthOffset");
    horizontalPassTexelHeightOffsetUniform = program2->uniformIndex("texelHeightOffset");
    
    setupFilterForSize(320, 480);
}

void LBTwoPassTextureSamplingFilter::setupFilterForSize(int width, int height){
    verticalPassTexelWidthOffset = 0.0;
    verticalPassTexelHeightOffset = 1.0 / width;
    horizontalPassTexelWidthOffset = 1.0 / height;
    horizontalPassTexelHeightOffset = 0.0;
    
    LBGraphics* g = LBGraphics::getInstance();
    
    g->useProgram(program);
    glUniform1f(verticalPassTexelWidthOffsetUniform, verticalPassTexelWidthOffset);
    glUniform1f(verticalPassTexelHeightOffsetUniform, verticalPassTexelHeightOffset);
    
    g->useProgram(program2);
    glUniform1f(horizontalPassTexelWidthOffsetUniform, horizontalPassTexelWidthOffset);
    glUniform1f(horizontalPassTexelHeightOffsetUniform, horizontalPassTexelHeightOffset);
}

void LBTwoPassTextureSamplingFilter::setTexture(LBTexture* texture){
    LBTwoPassFilter::setTexture(texture);
    setupFilterForSize(texture->getWidth(), texture->getHeight());
}